package com.shadt.core.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shadt.core.dao.RoleDao;
import com.shadt.core.dao.UserDao;
import com.shadt.core.entity.Role;
import com.shadt.core.entity.User;
import com.shadt.core.service.RoleService;
import com.shadt.core.util.SessionInfo;
import com.shadt.core.vo.RoleVo;

/**
 * 角色业务层实现类
 * 
 * 此类用于对角色信息的操作
 * 
 * @author SongR
 * 
 */
@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	RoleDao roleDao;
	@Autowired
	UserDao userDao;

	/**
	 * 实现根据当前登陆用户获取拥有的角色信息
	 */
	public List<Role> getByUser(SessionInfo sessionInfo) {
//		Map<String, Object> params = new HashMap<String, Object>();// 查询条件
//		List<Role> roleList = new ArrayList<Role>();// 角色列表
		return null;
	}

	/**
	 * 实现获取角色
	 */
	public List<RoleVo> getRole(String id) {
		List<RoleVo> roleVoList = new ArrayList<RoleVo>();
		List<Role> roleList = roleDao.find("from Role order by seq");
		if (roleList.size() > 0) {
			for (Role role : roleList) {
				RoleVo roleVo = new RoleVo();
				roleVo.setId(role.getId());
				roleVo.setName(role.getName());
				for (User user : role.getUsers()) {
					if (user.getId().equals(id)) {
						roleVo.setCheck(true);
					}
				}
				roleVoList.add(roleVo);
			}
		}
		return roleVoList;
	}

	/**
	 * 实现用户授权
	 */
	public void addRole(String roleIds, String userId) {
		User user = userDao.get(User.class, userId);
		Set<Role> rSet = new HashSet<Role>();
		if (null != roleIds && roleIds.length() > 0 && !roleIds.equals("null")) {
			for (String id : roleIds.split(",")) {
				rSet.add(roleDao.get(Role.class, id));
			}
		}
		user.setRoles(rSet);
		userDao.save(user);
	}

	/**
	 * 实现添加角色
	 */
	public void addRole(RoleVo roleVo) {
		Role role = new Role();
		if (null != roleVo) {
			role.setId(UUID.randomUUID().toString());
			role.setName(roleVo.getName());
			role.setRemark(roleVo.getRemark());
			if (null != roleVo.getSeq())
				role.setSeq(Integer.parseInt(roleVo.getSeq().toString()));
			roleDao.saveOrUpdate(role);
		}
	}

	/**
	 * 实现获取所有角色
	 */
	public List<RoleVo> getAllRole(String sSearch, int pageNo, int rows) {
		List<RoleVo> ul = new ArrayList<RoleVo>();
		String hql = " from Role t ";
		List<Role> l = roleDao.find(hql + whereHql(sSearch), pageNo / rows + 1,rows);
		if (null != l && l.size() > 0) {
			for (Role t : l) {
				RoleVo u = new RoleVo();
				BeanUtils.copyProperties(t, u);
				ul.add(u);
			}
		}
		return ul;
	}

	/**
	 * 实现获取记录行数
	 */
	public Long getRoleCount(String sSearch) {
		return roleDao.count("select count(*) from Role t" + whereHql(sSearch));
	}
	/**
	 * 组装where条件
	 * 
	 * @param user
	 * @param params
	 * @return
	 */
	private String whereHql(String str) {
		String hql = " where 1=1";
		if (null != str && !"".equals(str)) {
			hql += " and  t.name like   '%" + str + "%' ";
		}
		return hql;
	}
}
